<script setup>
  import { reactive, onUpdated } from 'vue'
  import { GetUserSigninInfo, UserSignin } from '@/api/user'
  import useAppStatusStore from '@/stores/appStatus'
  import useUserInfoStore from '@/stores/userinfo'
  const appStatusStore = useAppStatusStore()
  const userInfoStore = useUserInfoStore()

  const activationObj = reactive({
    showDialog: appStatusStore.showSignin,
    activation: userInfoStore.activation,
    signupLoading: false,
    todayHasDone: false,
    signupData: [],
    dataLoading: false,
  })

  const setSiginDataList = (connDays) => {
    activationObj.signupData = [
      {
        index: 1,
        value: 10,
        signed: false
      },
      {
        index: 2,
        value: 15,
        signed: false
      },
      {
        index: 3,
        value: 20,
        signed: false
      },
      {
        index: 4,
        value: 30,
        signed: false
      },
      {
        index: 5,
        value: 35,
        signed: false
      },
      {
        index: 6,
        value: 40,
        signed: false
      },
      {
        index: 7,
        value: 50,
        signed: false
      }
    ].map((item) => {
      if (item.index <= connDays) {
        item.signed = true
      }
      return item
    })
  }

   // 点击签到
   const toSignUp = () => {
    activationObj.signupLoading = true
    UserSignin().then((res) => {
      setSiginDataList(res.data.conn_days)
      userInfoStore.activation = res.data.activation
      activationObj.todayHasDone = true
      activationObj.signupLoading = false
      setTimeout(() => {
        appStatusStore.showSignin = false
      }, 1000)
    })
  }

  onUpdated(() => {
    if (appStatusStore.showSignin) {
        // console.log('signinDialog onUpdating!')
        activationObj.dataLoading = true
        GetUserSigninInfo().then((res) => {
        setSiginDataList(res.data.conn_days)
        activationObj.todayHasDone = res.data.today_signin
        activationObj.dataLoading = false
        // console.log('signinDialog onUpdated!')
      }).catch(() => {
        appStatusStore.showSignin = false
      })
    }
  })

</script>
<template>
  <!-- 决策粒说明，签到对话框 -->
  <el-dialog
    v-model="appStatusStore.showSignin"
    width="700"
    title="决策粒"
  >
    <template #header>
      <div>
        <span>决策粒</span>
        <span class="activation-tip">表示帐户的活跃度值，通过【登录】【签到】获得，连签奖励更高，断签从第一天开始。用途敬请期待...</span>
      </div>
    </template>
    <div class="sign-box">
      <div class="sign-items">
        <div v-for="day in activationObj.signupData" :key="day.index" class="sign-item">
          <div>
            <div v-if="day.signed">
              <div class="day-icon day-siged"><el-icon><Check /></el-icon></div>
              <div class="day-value day-siged">+{{ day.value }}</div>
            </div>
            <div v-else>
              <div class="day-icon"><el-icon><Star /></el-icon></div>
              <div class="day-value">+{{ day.value }}</div>
            </div>
            
          </div>
          <div class="day-name">第{{ day.index }}天</div>
        </div>
      </div>
      <div class="actions">
        <el-button
          v-if="activationObj.dataLoading"
          class="signup"
          :loading="activationObj.dataLoading"
        >
          数据加载中...
        </el-button>
        <el-button
          v-else
          type="primary" 
          class="signup"
          :disabled="activationObj.todayHasDone"
          :loading="activationObj.signupLoading"
          @click="toSignUp"
        >
          {{ activationObj.todayHasDone ? '今日已签' : '立即签到' }}
        </el-button>
      </div>
    </div>
  </el-dialog>
</template>
<style scoped lang="scss">
  .activation-tip {
    font-size: 12px;
    color: #777;
    line-height: 18px;
    margin-left: 2px;
  }
.sign-box {
  .sign-items{
    display: flex;
    justify-content: space-between;
    text-align: center;
    margin: -20px 0 0 0;
    padding: 0 40px;
    .sign-item {
      width: 70px;
      .day-siged {
        background-color: #dd8144 !important;
      }
      .day-icon {
        line-height: 22px;
        font-size: 40px;
        color: #fff;
        padding-top: 10px;
        background-color: #999;
        border-radius: 10px 10px 0 0;
      }
      .day-value {
        color: #fff;
        font-size: 20px;
        background-color: #999;
        border-radius: 0 0 10px 10px;
      }
      
      .day-name {
        font-size: 15px;
        color: #636161;
      }
    }
    }
    .actions {
      text-align: center;
      margin-top: 16px;
      .signup {
        height: 40px;
        width: 200px;
        font-size: 17px;
      }
    }
}
</style>